package net.jiastudy.web.rest.controller;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.jiastudy.biz.sys.entity.Permission;
import net.jiastudy.biz.sys.util.ReaderPermissionConfig;
import net.jiastudy.biz.user.entity.Role;
import net.jiastudy.biz.user.entity.User;
import net.jiastudy.biz.user.service.RoleService;
import net.jiastudy.core.base.BaseController;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roles")
@Api(value = "RoleController  角色和权限管理")
public class RoleController extends BaseController {
	@Resource
	private RoleService roleService;

	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value = "创建角色", notes = "创建新的角色")
	public Long create(@RequestBody Role role) {
		User user = this.getCurrentUser();
		role.setCreatedUserId(user.getId());
		roleService.createRole(role);
		return role.getId();
	}

	@RequestMapping(path = "/{id}", method = RequestMethod.PUT)
	@ApiOperation(value = "编辑角色", notes = "编辑已有的角色")
	public void edit(@PathVariable Long id, @RequestBody Role role) {
		role.setId(id);
		roleService.updateRole(role);
	}

	@RequestMapping(path = "/checkcode", method = RequestMethod.GET)
	@ApiOperation(value = "检查角色代码是否重复", notes = "根据角色代码检查是否重复,true为可用，false为不可用")
	public Boolean checkCode(@RequestParam(required = true) String code, Long id) {
		return !roleService.checkCodeExists(code, id);
	}
	
	@RequestMapping(method = RequestMethod.GET)
	@ApiOperation(value = "分页检索角色列表", notes = "根据查询条件查询角色")
	public Page<Role> page(@RequestParam Map<String, String> conditions,
			@PageableDefault(value = 50, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
		Page<Role> page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
		Sort sort = pageable.getSort();
		return roleService.searchRoles(page, conditions, sort);
	}
	
	@RequestMapping(path = "/list", method = RequestMethod.GET)
	@ApiOperation(value = "获取课程列表")
	public List<Role> finds(String keywords) {
		return roleService.findRolesAll(keywords);
	}

	@RequestMapping(path = "/{id}", method = RequestMethod.GET)
	@ApiOperation(value = "获取角色信息", notes = "获取角色信息，包括角色所属的权限code")
	public Role get(@PathVariable Long id) {
		return roleService.getRoleById(id);
	}

	@RequestMapping(path = "/sys_permissions", method = RequestMethod.GET)
	@ApiOperation(value = "获取系统权限树", notes = "获取系统权限树")
	public List<Permission> getSysPermissions() {
		return ReaderPermissionConfig.readerConfig().getPermissions();
	}
}
